﻿using System.Security.AccessControl;

namespace System.IO
{
    public class InMemoryFileInfo : InMemoryFileSystemEntryInfo, IFileInfo
    {
        public InMemoryFileInfo(InMemoryFileSystemStore store, InMemoryFileSystemEntryProperties entry) : base(store, entry)
        {
        }

        public InMemoryFileInfo(InMemoryFileSystemStore store, string name) : base(store, name)
        {
        }

        public override string Name
        {
            get { return LongPathTools.Instance.GetFileName(FullName); }
        }

        public FileSecurity GetAccessControl()
        {
            throw new System.NotImplementedException();
        }

        public FileSecurity GetAccessControl(AccessControlSections includeSections)
        {
            throw new System.NotImplementedException();
        }

        public void SetAccessControl(FileSecurity fileSecurity)
        {
            throw new System.NotImplementedException();
        }

        public StreamReader OpenText()
        {
            return new StreamReader(OpenRead());
        }

        public StreamWriter CreateText()
        {
            return new StreamWriter(OpenWrite());
        }

        public StreamWriter AppendText()
        {
            return new StreamWriter(Open(FileMode.Append));
        }

        public IFileInfo CopyTo(string destFileName)
        {
            return Store.CopyFile(ExistingEntry.FullPath, destFileName, false);
        }

        public IFileInfo CopyTo(string destFileName, bool overwrite)
        {
            return Store.CopyFile(ExistingEntry.FullPath, destFileName, false);
        }

        public Stream Create()
        {
            return Open(FileMode.Create);
        }

        public void Decrypt()
        {
            throw new NotSupportedException();
        }

        public void Encrypt()
        {
            throw new NotSupportedException();
        }

        public Stream Open(FileMode mode)
        {
            return Open(mode, FileAccess.ReadWrite);
        }

        public Stream Open(FileMode mode, FileAccess access)
        {
            return Open(mode, access, FileShare.None);
        }

        public Stream Open(FileMode mode, FileAccess access, FileShare share)
        {
            return Store.OpenFile(FullName, mode, access, share);
        }

        public Stream OpenRead()
        {
            return Open(FileMode.Open, FileAccess.Read);
        }

        public Stream OpenWrite()
        {
            return Open(FileMode.OpenOrCreate, FileAccess.Write);
        }

        public void MoveTo(string destFileName)
        {
            Store.MoveFileTo(ExistingEntry.FullPath, destFileName);
        }

        public IFileInfo Replace(string destinationFileName, string destinationBackupFileName)
        {
            return Store.Replace(ExistingEntry.FullPath, destinationFileName, destinationBackupFileName, false);
        }

        public IFileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
        {
            return Store.Replace(ExistingEntry.FullPath, destinationFileName, destinationBackupFileName, ignoreMetadataErrors);
        }

        public long Length
        {
            get { return ExistingEntry.Length; }
        }

        public string DirectoryName
        {
            get { return LongPathTools.Instance.GetDirectoryName(FullName); }
        }

        public IDirectoryInfo Directory
        {
            get { return new InMemoryDirectoryInfo(Store, DirectoryName); }
        }

        public bool IsReadOnly
        {
            get { return false; }
            set {  }
        }
    }
}